package com.lry.sso.auth.server.runner;

import com.lry.sso.auth.common.util.RsaKeyHelper;
import com.lry.sso.auth.server.bean.KeyConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @author 公钥生成
 * @create 2017/12/17.
 */
@Configuration
public class AuthServerRunner implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(AuthServerRunner.class);

    @Autowired
    private KeyConfiguration keyConfiguration;

    @Override

    public void run(String... args) throws Exception {
        logger.info("generateKey.....");
        // 生成用户公钥私钥
        Map<String, byte[]> keyMap = RsaKeyHelper.generateKey(keyConfiguration.getUserSecret());
        keyConfiguration.setUserPriKey(RsaKeyHelper.getPrivateKey(keyMap));
        keyConfiguration.setUserPubKey(RsaKeyHelper.getPublicKey(keyMap));
        logger.debug("用户私钥：{}",RsaKeyHelper.getPrivateKey(keyMap));
        logger.debug("用户公钥：{}",RsaKeyHelper.getPublicKey(keyMap));
//        redisTemplate.opsForValue().set(RedisConstants.REDIS_USER_PUB_KEY, RsaKeyHelper.toHexString(keyMap.get("pub")));
        // 生成微服务间调用公钥私钥
        keyConfiguration.setServicePriKey(keyMap.get("pri"));
        keyConfiguration.setServicePubKey(keyMap.get("pub"));
//        redisTemplate.opsForValue().set(RedisConstants.REDIS_SERVICE_PUB_KEY,
//            RsaKeyHelper.toHexString(keyMap.get("pub")));

        // }
        logger.info("generateKey end.....");
    }
}
